package info.ask4research.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import info.ask4research.dao.IRoleDAO;
import info.ask4research.dao.core.impl.AbstractDaoImpl;
import info.ask4research.model.UnitOfLearning;
import info.ask4research.model.User;
import info.ask4research.model.roles.Role;

@Repository("roleDAO")
public class RoleDAO extends AbstractDaoImpl<Role> implements IRoleDAO {

	public RoleDAO() {
		super(Role.class);
	}

	private HibernateTemplate hibernateTemplate;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	@Override
	public List<Role> getStaffRolesByUoL(int id) {
		return (List<Role>) hibernateTemplate.find("from Role role where role.unitOfLearning.id=? and role.staffRole=?",  id,true);
	}

	@Override
	public List<Role> getLearnerRolesByUoL(int id) {
		return (List<Role>) hibernateTemplate.find("from Role role where role.unitOfLearning.id=? and role.learnerRole=?",  id,true);
	}

	@Override
	public List<Role> getRolesByUoL(int id) {
		return (List<Role>) hibernateTemplate.find("from Role role where role.unitOfLearning.id=?",  id);
	}

	@Override
	public boolean roleNameExists(String stringRole, Integer uoLId) {
		List<Role> roles = (List<Role>) hibernateTemplate.find("from Role role where role.unitOfLearning.id=? and role.text=?",  uoLId,stringRole);
			if(roles.isEmpty()){
				return false;
			}else{
				return true;
			}
		}

	@Override
	public Role getRoleByUoFandRoleName(String stringRole, Integer uoLId) {
		List<Role> roles = (List<Role>) hibernateTemplate.find("from Role role where role.unitOfLearning.id=? and role.text=?",  uoLId,stringRole);
		return roles.get(0);
	}

	@Override
	public List<Role> getRoleListByUoL(final Integer limit, final Integer page, final Integer start, final Integer idUoL) {
		@SuppressWarnings("unchecked")
		List<Role> roles = hibernateTemplate.executeFind(new HibernateCallback() {  
            public Object doInHibernate(Session session) throws HibernateException, SQLException {  
	                 Query query = (Query) session.createQuery("from Role role where role.unitOfLearning.id=:idUoL");
	                 query.setParameter("idUoL", idUoL);
	                 query.setMaxResults(limit);  
	                 query.setFirstResult(start);  
	                 return query.list();  
	             }  
	         }); 
		return roles;
	}

	@Override
	public Role getRoleByName(String manager) {
		List<Role> roles = (List<Role>) hibernateTemplate.find("from Role role where role.text=?",  manager);
		return roles.get(0);
	}

}
